<script setup>
import { reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import api from '@/api'
import { watch } from 'vue'
import { Message } from '@arco-design/web-vue'

const $route = useRoute()
const $router = useRouter()

const listData = ref([])

const StatusList = reactive([
  {
    label: '待评价',
    value: 1,
  },
  {
    label: '已评价',
    value: 2,
  },
])

const departments = ref([])
const posts = ref([])

const filter = reactive({
  departments_id: '',
  posts_id: '',
  status: '',
  user_name: '',
  page: 1,
  pageSize: 10,
})

const formData = ref({
  template_id: null,
  posts_id: null,
})
const formVisible = ref(false)
const templateAllList = ref([])
const postsOptionList = ref([])
// const filterCurrentDay = () => {
//   if (filter.day) {
//     filter.day = ''
//   } else {
//     const now = new Date()
//     filter.day = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`
//   }

//   getListData()
// }

const getListData = async () => {
  try {
    const res = await api.get('/evaluate/myEvaluateList', {
      params: filter,
    })
    console.log('res', res)
    listData.value = res.data.items
    // listTotal.value = res.total
  } catch (error) {
    console.error(error)
  }
}

// 部门列表
const getDepartments = async () => {
  console.log('获取部门列表')
  const res = await api.get('/evaluate/departmentsList')
  departments.value = res.data
  // if (res.data.length > 0) {
  //   getPosts(res.data[0].id)
  // }
}

// 岗位列表
const getPosts = async (departId) => {
  console.log('获取岗位列表' + departId)
  const res = await api.get(`/evaluate/postsList?department_id=${departId}`)
  posts.value = res.data
}

const toJump = (url) => {
  console.log('router', $router.push(url))
  $router.push(url)
}

watch(
  () => filter.departments_id,
  (newVal) => {
    console.log('change departments_id', newVal)
    getPosts(newVal)
  }
)

const getPostsOptions = async () => {
  try {
    const res = await api.get('system/postsOptions')
    console.log('res', res)
    postsOptionList.value = res.data
  } catch (error) {
    console.error('error', error)
    return false
  }
}

const getTemplateAllList = async () => {
  try {
    const res = await api.get('/evaluate/templateAllList')
    console.log('res', res)
    templateAllList.value = res.data.items
  } catch (error) {
    console.error(error)
  }
}

const showFormData = async () => {
  resetFormData()
  formVisible.value = true
}

const formSubmit = async () => {
  var obj = { ...formData.value }
  if (!obj.posts_id) {
    Message.error('请选择岗位')
    return false
  }
  if (!obj.template_id) {
    Message.error('请选择模板')
    return false
  }
  try {
    const res = await api.post('/evaluate/createMyEvaluate', obj)
    console.log('res', res)
    Message.success(res.message)
    getListData(1)
    resetFormData()
  } catch (error) {
    console.error(error)
  }
}

const resetFormData = async () => {
  formData.value = {
    template_id: null,
    posts_id: null,
  }
}

getListData()
getDepartments()
getTemplateAllList()
getPostsOptions()
</script>

<template>
  <div class="page">
    <div class="header">
      <a-space>
        <!-- <a-button type="primary">今日评价</a-button>
        <a-button type="primary">应聘人员模糊查询</a-button>
        <a-button type="primary">所属部门筛选</a-button>
        <a-button type="primary">所属岗位筛选</a-button>
        <a-button type="primary">评价状态筛选</a-button> -->

        <!-- <a-button type="primary"
                  :status="filter.day ? 'warning' : 'primary'"
                  @click="filterCurrentDay">{{ filter.day ? '清除日期' : '今日评价' }}</a-button> -->
        <!-- <a-date-picker type="date" placeholder="请选择日期" v-model="filter.day" format="YYYY-MM-DD"></a-date-picker> -->
        <a-select v-model="filter.departments_id"
                  placeholder="请选择招聘部门"
                  allow-clear>
          <a-option v-for="(item, index) in departments"
                    :key="index"
                    :value="item.id">{{ item.name }}</a-option>
        </a-select>
        <a-select v-model="filter.posts_id"
                  placeholder="请选择招聘岗位名称"
                  allow-clear>
          <a-option v-for="(item, index) in posts"
                    :key="index"
                    :value="item.id">{{ item.name }}</a-option>
        </a-select>
        <a-select v-model="filter.status"
                  placeholder="评价状态">
          <a-option value="">选择评价状态</a-option>
          <a-option v-for="item in StatusList"
                    :value="item.value"
                    :key="item.value">{{ item.label }}</a-option>
        </a-select>
        <a-input-search :style="{ width: '320px' }"
                        v-model="filter.user_name"
                        placeholder="名称模糊查询"
                        button-text="查询"
                        @search="getListData"
                        search-button />
      </a-space>

      <a-button @click="showFormData()"
                type="primary">
        创建评价
      </a-button>
    </div>

    <a-table :data="listData">
      <template #columns>
        <a-table-column title="序号"
                        :width="60">
          <template #cell="{ rowIndex }"> {{ rowIndex + 1 }} </template>
        </a-table-column>
        <a-table-column title="所属部门"
                        data-index="departments"></a-table-column>
        <a-table-column title="岗位名称"
                        data-index="posts"></a-table-column>
        <a-table-column title="评价对象"
                        data-index="user_name"></a-table-column>
        <a-table-column :sortable="{ sortDirections: ['ascend', 'descend'] }"
                        title="预约评价时间"
                        data-index="evaluate_time"></a-table-column>
        <a-table-column :sortable="{ sortDirections: ['ascend', 'descend'] }"
                        title="评价状态">
          <template #cell="{ record }">
            <div v-if="record.status==1">待评价</div>
            <div v-if="record.status==2">已评价</div>
          </template>
        </a-table-column>
        <a-table-column title="操作"
                        align="right"
                        :width="200">
          <template #cell="{ record }">
            <a-space>
              <a-button v-if="record.status == 1"
                        type="primary"
                        size="mini"
                        @click="
                  toJump(
                    `/evaluate/myEvaluateDetails?evaluate_id=${record.id}&type=1`
                  )
                ">开始评价</a-button>
              <a-button v-if="record.status == 2"
                        type="primary"
                        size="mini"
                        @click="
                  toJump(
                    `/evaluate/myEvaluateDetails?evaluate_id=${record.id}`
                  )
                ">评价详情</a-button>
            </a-space>
          </template>
        </a-table-column>
      </template>
    </a-table>

    <a-modal title="评价详情"
             width="600px"
             :on-before-ok="formSubmit"
             v-model:visible="formVisible">
      <a-form>
        <a-form-item label="岗位">
          <a-select v-model="formData.posts_id"
                    :style="{width:'400px'}"
                    placeholder="请选择岗位">
            <a-optgroup :label="item.name"
                        v-for="(item,index) in postsOptionList"
                        :key="index">
              <a-option :value="items.id"
                        v-for="(items,indexs) in item.posts"
                        :key="indexs">
                {{ items.name }}
              </a-option>
            </a-optgroup>
          </a-select>
        </a-form-item>
        <a-form-item label="评价模板">
          <a-select v-model="formData.template_id"
                    :style="{width:'400px'}"
                    placeholder="请选择评价模板">
            <a-option :value="item.id"
                      v-for="(item,index) in templateAllList"
                      :key="index">
              {{ item.template_name }}
            </a-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<style scoped>
.page {
  height: 95%;
}

.header {
  height: 52px;
  display: flex;
  justify-content: space-between;
}
</style>
